package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.sms.SmsCouponHistoryMapper;
import com.itheima.pojo.sms.SmsCouponHistory;
import com.itheima.service.sms.SmsCouponHistoryService;
import com.itheima.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SmsCouponHistoryServiceImpl extends ServiceImpl<SmsCouponHistoryMapper, SmsCouponHistory> implements SmsCouponHistoryService {


    /**
     *根据优惠券id，使用状态，订单编号分页获取领取记录
     * @param couponId
     * @param orderSn
     * @param pageNum
     * @param pageSize
     * @param useStatus
     * @return
     */
    public PageInfo historyRecord(Integer couponId, String orderSn, Integer pageNum, Integer pageSize, Integer useStatus) {

        Page<SmsCouponHistory> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(couponId != null,SmsCouponHistory::getCouponId,couponId)
                .eq(useStatus != null, SmsCouponHistory::getUseStatus,useStatus)
                .eq(StringUtils.isNotEmpty(orderSn), SmsCouponHistory::getOrderSn,orderSn);

        Page<SmsCouponHistory> historyRecordPage = this.page(page, lqw);
        int total = (int) historyRecordPage.getTotal();
        List<SmsCouponHistory> historyList = historyRecordPage.getRecords();
        Integer totalPage = PageInfo.totalPage(pageSize, total);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(historyList);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(totalPage);

        return pageInfo;
    }
}
